package com.IO.ReadAndWrite;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/***
 * @ClassName: ReadWrite
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 0:19 2022/2/7
 * @version : V1.0
 */
public class ReadWrite {

    public static void main(String[] args) {
        //read();
        //write();
        copy();
    }
    public static void read(){
        try {
            FileReader fileReader = new FileReader("d:\\aaa.txt");
            int read = 0;
            //读取一个字符
/*             while (((read = fileReader.read()) != -1)) {
                System.out.print((char)read);
            }*/

            //创建缓冲区2个字符长度
            char [] buf = new char[2];
            while (((read = fileReader.read(buf)) != -1)) {
                System.out.print(new String(buf,0,read));
            }
            fileReader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write() {
        try {
            FileWriter fileWriter = new FileWriter("d:\\ccc.txt");
            fileWriter.write("大记号");
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //复制文件
    public static void copy() {
        try {
            FileReader fileReader = new FileReader("d:\\aaa.txt");
            FileWriter fileWriter = new FileWriter("d:\\aaacopy.txt");
            int read = 0;
            //创建缓冲区2个字符长度
            char [] buf = new char[2];
            while (((read = fileReader.read(buf)) != -1)) {
                fileWriter.write(new String(buf,0,read));
                System.out.print(new String(buf,0,read));
            }
            fileReader.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
